package com.itheima.stock.service.impl;

import com.itheima.stock.domain.StockUpDownListDomain;
import com.itheima.stock.domain.StockUpdownDomain;
import com.itheima.stock.mapper.StockMarketIndexInfoMapper;
import com.itheima.stock.mapper.StockRtInfoMapper;
import com.itheima.stock.pojo.dto.CountAndTimeDto;
import com.itheima.stock.service.StockUpDownService;
import com.itheima.stock.utils.DateTimeUtil;
import com.itheima.stock.vo.resp.R;
import com.itheima.stock.vo.resp.StockInfo;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName StockUpDownServiceImpl
 * @Description TODO
 * @date 2023/3/2 16:37
 * @Version 1.0
 */
@Service
public class StockUpDownServiceImpl implements StockUpDownService {
    @Autowired
    private StockRtInfoMapper stockRtInfoMapper;
    @Autowired
    private StockMarketIndexInfoMapper stockMarketIndexInfoMapper;

    /**
     * 个股涨跌
     * @return
     */
    @Override
    public R<Map> getStockUpdown() {
        String time = "20220106095500";
        List<Map<String,Integer>> map =  stockRtInfoMapper.getStockUpdown(time);
        HashMap<String, List> resmap = new HashMap<>();
        Map<String,String> hashMap = new HashMap<>();
        hashMap.put("time",time);
        ArrayList<String> maps = new ArrayList<>();
        maps.add(time);
        resmap.put("time",maps);
        resmap.put("infos",map);
        return R.ok(resmap);
    }

    @Override
    public R<StockUpDownListDomain> getStockUpdownDomain() {
        Integer up = 0;
        Integer down = 0;

        Date timeStart = new Date();
        Date timeEnd = new Date();
        //TODO 造时间 1.获取股票最近的有效交易日期,精确到秒
        DateTime curDateTime = DateTimeUtil.getLastDate4Stock(DateTime.now());
        //当前最近有效期
        Date curTime = curDateTime.toDate();
        //开盘日期
        Date openTime = DateTimeUtil.getOpenDate(curDateTime).toDate();
        // mock_data  模拟后续数据实时获取时，注释掉
        String curTimeStr="20220106142500";
        //2022-01-06 09:30:00
        //对应开盘日期 mock_data
        String openTimeStr="20220106092500";
        curTime = DateTime.parse(curTimeStr, DateTimeFormat.forPattern("yyyyMMddHHmmss")).toDate();
        openTime = DateTime.parse(openTimeStr, DateTimeFormat.forPattern("yyyyMMddHHmmss")).toDate();
        //TODO
        List<CountAndTimeDto> upList =  stockRtInfoMapper.selectStockUp(up,openTime,curTime);
        List<CountAndTimeDto> downList =  stockRtInfoMapper.selectStockDown(down,openTime,curTime);

        StockUpDownListDomain stockUpDownListDomain = new StockUpDownListDomain();
        stockUpDownListDomain.setUpList(upList);
        stockUpDownListDomain.setDownList(downList);


        return R.ok(stockUpDownListDomain);
    }

    /**
     * 成交量查询
     * @return
     */
    @Override
    public R<Map>getTradevol() {

        String enfTime = "2021-12-28 15:00:00";
        String enfTime2 = "2021-12-28 15:00:00";
        String openTime = "2021-12-27 09:30:00";
        String openTime2 = "2021-12-27 09:30:00";
        List<String> inner = new ArrayList<>();
        inner.add("s_sh000001");
        inner.add("s_sz399001");
        System.out.println(inner);
       List<Map> data =   stockMarketIndexInfoMapper.getTradevol(inner,openTime,enfTime);
       List<Map> data2 =   stockMarketIndexInfoMapper.getTradevol(inner,openTime2,enfTime2);
        HashMap<String ,List<Map>> map = new HashMap<>() ;
        map.put("volList",data);
        map.put("yesVolList",data2);
        return R.ok(map);
    }


}
